package com.znfeiche.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PropertiesUtil {
	public static void checkFileExit(String filePath){
		File file = new File(filePath);
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	public static String read(String filePath,String key){
		Properties properties = new Properties();
		File file = new File(filePath);
		FileInputStream fis =null;
		try {
			fis = new FileInputStream(file);
			properties.load(new InputStreamReader(fis,"UTF-8"));
			String value = properties.getProperty(key);
			return value;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(fis,null);
		}
		return null;
		
	}
	public static Map<String,String> getParams(String filePath){
		Properties properties = new Properties();
		File file = new File(filePath);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			properties.load(new InputStreamReader(fis,"UTF-8"));
			Map<String,String> map = new HashMap(properties);
			return map;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(fis,null);
		}
		return null;
	}
	public static void write(String filePath,String key,String value) {
		Properties properties = new Properties();
		File file = new File(filePath);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file);
			properties.load(new InputStreamReader(fis,"UTF-8"));
			Map<String,String> map = new HashMap<String,String>();
			Set<Object>keySet = properties.keySet();
			for(Object object:keySet){
				String keytmp = (String) object;
				String valuetmp = properties.getProperty(keytmp);
				map.put(keytmp, valuetmp);
			}
			map.put(key, value);
			Set<Entry<String,String>> entrySet = map.entrySet();
			for(Entry<String,String> entry:entrySet){
				properties.setProperty(entry.getKey(), entry.getValue());
			}
			fos = new FileOutputStream(file);
			properties.store(fos, "update");
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(fis,fos);
		}
		
		
	}
	public static void delete(String filePath,String key){
		Properties properties = new Properties();
		File file = new File(filePath);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(file);
			properties.load(new InputStreamReader(fis,"UTF-8"));
			properties.remove(key);
			fos = new FileOutputStream(file);
			properties.store(fos, "update");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			close(fis,fos);
		}
		
	}
	public static void close(FileInputStream fis,FileOutputStream fos){
		if(fis!=null){
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(fos!=null){
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
